package org.agilelang.editor.syntaxtext.dialogpopup;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import org.agilelang.object.Model;
import org.agilelang.object.Project;
import org.agilelang.object.attributes.Attribute;

public class DlgScaffoldAddRelation extends DialogBase {

    JComboBox name_model = new JComboBox();
    JTextField name_key = new JTextField(25);
    JTextField foreign_key = new JTextField(25);
    JComboBox class_reference = new JComboBox();
    JComboBox attribute_reference = new JComboBox();
    JComboBox field_reference = new JComboBox();
    JTextField description = new JTextField(25);
    Project project;

    public DlgScaffoldAddRelation(Project project) {
        super("Scaffold : addRelation()", 9);
        this.project = project;
        initComponents();
        initUI();
    }

    private void initComponents() {

        List<Model> listm = project.models;
        //List<Scaffold> lists = project.scaffolds;
        int t = listm.size();
        //int v = lists.size();
        String[] arry = new String[t];// + v];
        for (int i = 0; i < t; i++) {
            Model model = listm.get(i);
            name_model.addItem(model.name);
            class_reference.addItem(model.name);
        }
        /*for (int i = 0; i < v; i++) {
            Scaffold scaffold = lists.get(i);
            name_model.addItem(scaffold.name);
            class_reference.addItem(scaffold.name);
        }*/

        class_reference.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                System.out.println(e.getItem());
                foreign_key.setText(e.getItem().toString().toLowerCase()+"_id");
                attribute_reference.removeAllItems();
                field_reference.removeAllItems();
                attribute_reference.addItem("id");
                List<Model> listm = project.models;
                //List<Scaffold> lists = project.scaffolds;
                int t = listm.size();
                //int v = lists.size();
                for (int i = 0; i < t; i++) {
                    Model model = listm.get(i);
                    if (e.getItem().toString().endsWith(model.name)) {
                        List<Attribute> lista = model.attributes;
                        int w = lista.size();
                        for (int j = 0; j < w; j++) {
                            Attribute attr = lista.get(j);
                            attribute_reference.addItem(attr.name);
                            field_reference.addItem(attr.name);
                            return;
                        }
                    }
                }
                /*for (int i = 0; i < v; i++) {
                    Scaffold scaffold = lists.get(i);
                    if (e.getItem().toString().endsWith(scaffold.name)) {
                        List<Attribute> lista = scaffold.attributes;
                        int w = lista.size();
                        for (int j = 0; j < w; j++) {
                            Attribute attr = lista.get(j);
                            attribute_reference.addItem(attr.name);
                            field_reference.addItem(attr.name);
                            return;
                        }
                    }
                }*/
            }
        });

        this.addLineTxt("name_model", name_model);
        this.addLineTxt("name_key", name_key);
        this.addSapce();
        this.addLineTxt("class_reference", class_reference);
        this.addLineTxt("attribute_reference", attribute_reference);
        this.addLineTxt("field_reference", field_reference);
        this.addSapce();
        this.addLineTxt("foreign_key", foreign_key);
        this.addLineTxt("description", description);
    }

    public String getSentences() {
        return "Scaffold." + name_model.getSelectedItem().toString() + ".addReference(\n" 
                + "  name_key =\"" + name_key.getText() + "\", \n"
                + "  foreign_key =\"" + foreign_key.getText() + "\", \n"
                + "  class_reference = \"" + class_reference.getSelectedItem().toString() + "\", \n"
                + "  attribute_reference =\"" + attribute_reference.getSelectedItem().toString() + "\", \n"
                + "  field_reference =\"" + field_reference.getSelectedItem().toString() + "\", \n"
                + "  description=\"" + description.getText() + "\"\n"
                + ");\n";

    }

    public void setNameModels(String[] items) {
        int size = items.length;
        for (int i = 0; i < size; i++) {
            name_model.addItem(items[i]);
            class_reference.addItem(items[i]);
        }
    }
}